package com.travel.travelnetwork.controller.back;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.travel.travelnetwork.pojo.Permission;
import com.travel.travelnetwork.pojo.Role;
import com.travel.travelnetwork.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(("/back/permission"))
public class PermissionController {
    @Autowired
    private PermissionService permissionService;
    //查询所有角色
    @RequestMapping("/all")
    @PreAuthorize("hasAnyAuthority('/permission/all')")
    public ModelAndView all(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "10")int size) {
        Page<Permission> permissionPage = permissionService.findPage(page, size);
        ModelAndView mav = new ModelAndView();
        mav.addObject("permissionPage", permissionPage);
        mav.setViewName("/back/permission_all");
        return mav;
    }
    //新增角色
    @RequestMapping("/add")
    public String addPermission(Permission permission) {
        permissionService.addPermission(permission);
        return "redirect:/back/permission/all";
    }

    //查询角色
    @RequestMapping("/edit")
    public ModelAndView editPermission(Integer pid) {
        ModelAndView mav = new ModelAndView();
        Permission permission=permissionService.findPermissionById(pid);
        mav.addObject("permission",permission);
        mav.addObject("pid",pid);
        mav.setViewName("/back/permission_edit");
        return mav;
    }
    //修改角色
    @RequestMapping("/update")
    public String updatePermission(Permission permission) {
        permissionService.updatePermission(permission);
        return "redirect:/back/permission/all";
    }

    //删除角色
    @RequestMapping("/delete")
    public String deletepermission(Integer pid) {
        permissionService.deletePermission(pid);
        return "redirect:/back/permission/all";
    }

}
